import 'package:flutter/material.dart';
import '../../http/Api.dart';
import 'package:provider/provider.dart';
import '../goods/SkuProvider.dart';
import '../../base/Constant.dart';
class SkuDialog extends StatelessWidget {
  String goods_name = "";
  String initPrice = "";
  String img = "";
  String count = "";
  SkuDialog(
      {Key key,
      @required this.goods_name,
      @required this.initPrice,
      @required this.img})
      : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Align(
      alignment: Alignment.bottomCenter,
      child: Material(
          color: Colors.transparent,
          child: Consumer<SkuProvider>(builder: (context, provider, _) {
            return Container(
              constraints: BoxConstraints(minWidth: double.infinity),
              child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Container(
                      color: Colors.white,
                      child: new Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          provider.selectedSkuModel == null
                              ? (img == ""
                                  ? Image.asset('images/pic_default.png',
                                      width: 80, height: 80, fit: BoxFit.fill)
                                  : Image.network(
                                      Api.ImgBaseUrl + img.replaceAll('\\', '/'),
                                      width: 80,
                                      height: 80,
                                      fit: BoxFit.fill,
                                    ))
                              : (provider.selectedSkuModel.img == ""
                                  ? Image.asset('images/pic_default.png',
                                      width: 80, height: 80, fit: BoxFit.fill)
                                  : Image.network(
                                      Api.ImgBaseUrl +
                                          provider.selectedSkuModel.img.replaceAll('\\', '/'),
                                      width: 80,
                                      height: 80,
                                      fit: BoxFit.fill,
                                    )),
                          Expanded(
                            flex: 1,
                            child: Container(
                              padding: EdgeInsets.fromLTRB(10, 0, 5, 5),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  Text(
                                    goods_name,
                                    maxLines: 2,
                                    overflow: TextOverflow.ellipsis,
                                    style: TextStyle(
                                        fontSize: 14,
                                        color: black,
                                        fontWeight: FontWeight.w600),
                                  ),
                                  Padding(
                                    padding: EdgeInsets.fromLTRB(0, 5, 0, 0),
                                    child: provider.selectedSkuModel == null
                                        ? Text('')
                                        : Text(
                                            "库存:" +
                                                provider
                                                    .selectedSkuModel.inventory
                                                    .toString(),
                                            style: TextStyle(
                                                fontSize: 14, color: black),
                                          ),
                                  ),
                                  Padding(
                                    padding: EdgeInsets.fromLTRB(0, 5, 0, 0),
                                    child: Text(
                                      provider.selectedSkuModel == null
                                          ? initPrice+"积分" 
                                          : 
                                              provider.selectedSkuModel.price
                                                  .toString()+"积分" ,
                                      style: TextStyle(
                                          fontSize: 16, color: mainColor),
                                    ),
                                  )
                                ],
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
                      child: Text(provider.selectedSku),
                    ),
                    Wrap(
                      alignment: WrapAlignment.start,
                      runSpacing: 12,
                      children: create2(provider.groups),
                    ),
                  ]),
            );
          })),
    );
  }
}

List<Widget> create2(List<GroupProvider> radios) {
  List<ChangeNotifierProvider<GroupProvider>> buttos = new List();
  for (var radio in radios) {
    buttos.add(ChangeNotifierProvider<GroupProvider>.value(
      value: radio,
      child: _RadioGroupItem(),
    ));
  }
  return buttos;
}

class _RadioGroupItem extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var group = Provider.of<GroupProvider>(context);
    return Container(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
              margin: EdgeInsets.fromLTRB(0, 10, 0, 0),
              child: Text(group.groupName,
                  style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold))),
          Wrap(
            alignment: WrapAlignment.start,
            runSpacing: 12,
            children: create(group.radios),
          ),
        ],
      ),
    );
  }
}

List<Widget> create(List<RadioProvider> radios) {
  List<ChangeNotifierProvider> buttos = new List();
  for (var radio in radios) {
    buttos.add(ChangeNotifierProvider<RadioProvider>.value(
      value: radio,
      child: _RadioButton(),
    ));
  }
  return buttos;
}

class _RadioButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var radio = Provider.of<RadioProvider>(context);
    var group = Provider.of<GroupProvider>(context);
    bool isSelect = radio.radioName == group.selectedValue;
    return Container(
      margin: EdgeInsets.only(top: 8, right: 5),
      height: 28,
      child: FlatButton(
        child: Text(radio.radioName, style: TextStyle(fontSize: 12)),
        onPressed: radio.isDisabled
            ? null
            : () {
                if (group.selectedValue != radio.radioName) {
                  //选中
                  group.changeSelectValue(radio.radioName);
                } else {
                  //重复点击取消选中
                  group.changeSelectValue(null);
                }
              },
        color: isSelect ? Color(0x1AFF6631) : Color(0xFFF7F7F7),
        textColor: isSelect ? Color(0xFFFF6732) : Color(0xff333333),
        disabledTextColor: Color(0xff999999),
        shape: StadiumBorder(
            side: BorderSide(
                style: BorderStyle.solid,
                color: isSelect ? Color(0xFFFF6732) : Color(0xFFF7F7F7))),
      ),
    );
  }
}
